<?php
namespace app\admin\model;

use cubox\Model;

class UserKey extends Model
{
    public function __construct()
    {
        $this->table = 'admin_user_key';
    }

    public function getValue($user_id,$keyname){
        $this->where('user_id',$user_id);
        $this->where('key',$keyname);
        $data =  $this->getRow();

        if ($data){
            return $data['value'];
        }else{
            return '';
        }
    }

    public function setValue($user_id,$keyname,$value){
        $this->where('user_id',$user_id);
        $this->where('key',$keyname);
        if ($data = $this->getRow()){
            $this->updateId($data['user_key_id'],['value' => $value]);
        }else{
            $this->insert(['user_id' => $user_id,'key' => $keyname,'value' => $value]);
        }
    }

    public function deleteValue($user_id,$keyname){
        $this->where('user_id',$user_id);
        $this->where('key',$keyname);
        return $this->delete();
    }

    public function deleteUser($user_id){
        $this->where('user_id',$user_id);
        return $this->delete();
    }

    public function deleteKey($keyname){
        $this->where('key',$keyname);
        return $this->delete();
    }

    public function user($user_id){
        $this->where('user_id',$user_id);
        return $this->getData();
    }
}